//
//  HFAddressViewController.m
//  APPStart
//
//  Created by 慧鼎科技 on 2018/11/10.
//  Copyright © 2018 WQ. All rights reserved.
//

#import "HFAddressViewController.h"
#import "HFAddressTableViewCell.h"
#import "HFAddAddressViewController.h"
#import "HFAddressModel.h"
#import "CSPersonModel.h"

@interface HFAddressViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property(nonatomic,strong)CSPersonModel *model;

@end

@implementation HFAddressViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    
    [self requestData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.model = [[CSPersonModel alloc]init];
    [self.view addSubview:self.tableView];
    
    [self configNavi];
}

- (void)requestData {
    [self.dataSource removeAllObjects];
    kWeakSelf;
    [NetWorkTool POST:CcentenInfo param:nil success:^(id dic) {
        
        [self.model setValuesForKeysWithDictionary:[dic objectForKey:@"data"]];
        
        HFAddressModel *model = [[HFAddressModel alloc] init];
        model.user_name =self.model.name;
        model.phone =self.model.phone;
        model.address =self.model.address;
        model.areaAdd =self.model.areaAdd;
        model.area =self.model.area;
        
        if (!strIsEmpty(model.address)) {
            [self.dataSource addObject:model];
        }
        [weakSelf.tableView reloadData];
        
        self.rightbutton.hidden = YES;
        
        if (self.dataSource.count > 0) {
            self.rightbutton.hidden = YES;
        }
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        self.rightbutton.hidden = NO;
    } fail:^(NSError *error) {
        self.rightbutton.hidden = NO;
    } needUser:YES];
}

- (void)configNavi {
    [self.leftbutton setImage:imgname(@"mblack") forState:(UIControlStateNormal)];
    self.title = @"地址管理";
    [self.rightbutton setTitle:@"添加" forState:UIControlStateNormal];
    [self.rightbutton setTitleColor:[UIColor colorWithHexString:@"333333"] forState:UIControlStateNormal];
}

#pragma mark -- click点击事件
- (void)rightClick {
    HFAddAddressViewController *addAddress = [[HFAddAddressViewController alloc] init];
    [self.navigationController pushViewController:addAddress animated:YES];
}

#pragma mark -- tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HFAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HFAddressTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    HFAddressModel *model = [self.dataSource safeObjectAtIndex:indexPath.row];
    [cell setModel:model];
    
    kWeakSelf;
    cell.editOrDelBlock = ^(NSInteger index) {
        if (index == 0) { // 编辑
            HFAddAddressViewController *edit = [[HFAddAddressViewController alloc] init];
            edit.model = model;
            [weakSelf.navigationController pushViewController:edit animated:YES];
        } else if (index == 1) { // 删除
            [weakSelf requestData];
        }else if (index == 2) { // 默认地址
            [weakSelf requestData];
        }
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.chooseAddressBlock) {
        HFAddressModel *model = [self.dataSource safeObjectAtIndex:indexPath.row];
        self.chooseAddressBlock(model);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark -- 懒加载
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNaviHeight, ScreenW, ScreenH-kNaviHeight) style:(UITableViewStylePlain)];
        _tableView.backgroundColor = [UIColor colorWithHexString:@"eeeeee"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 40, 0);
        if (iOS11) {
            self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.tableFooterView = [UIView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 120;
        
        _tableView.placeHolderView = [[KKTableViewNoDataView alloc]initWithFrame:_tableView.bounds image:imgname(@"tableviewPlaseholder") viewClick:^{
            [self->_tableView.mj_header beginRefreshing];
        }];
        [_tableView registerNib:[UINib nibWithNibName:@"HFAddressTableViewCell" bundle:nil] forCellReuseIdentifier:@"HFAddressTableViewCell"];
    }
    return _tableView;
}

- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataSource;
}

@end
